<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('修改基础业务')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-base-edit" th:object="${business}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">业务类型：</label>
                <div class="col-sm-8">
                    <select name="businessType" class="form-control m-b" th:with="type=${@dict.getType('sd_business_type')}" required>
                        <option th:each="dict : ${type}" th:if="${@permission.isAnyRoles('admin')} or ${businessTypes} and ${#lists.contains(businessTypes, dict.dictValue)}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{businessType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group" th:hidden="*{businessType} ne 0">
                <label class="col-sm-3 control-label">充值通道：</label>
                <div class="col-sm-8">
                    <select name="rechargeChannel" class="form-control m-b" th:with="type=${@dict.getType('sd_recharge_channel')}">
                        <option th:each="dict : ${type}" th:if="${@permission.isAnyRoles('admin')} or ${rechargeChannels} and ${#lists.contains(rechargeChannels, dict.dictValue)}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{rechargeChannel}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">业务编号：</label>
                <div class="col-sm-8">
                    <input name="businessCode" th:field="*{businessCode}" class="form-control" type="text" required readonly>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">合作公司：</label>
                <div class="col-sm-8">
                    <input name="company" th:field="*{company}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">联系人：</label>
                <div class="col-sm-8">
                    <input name="contactPerson" th:field="*{contactPerson}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">联系方式：</label>
                <div class="col-sm-8">
                    <input name="mobile" th:field="*{mobile}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">业务价格：</label>
                <div class="col-sm-8 priceDiv">
                    <div class="col-sm-4" th:each="detail : *{prices}">
                        <label class="control-label" th:text="${detail.label}"></label><input class="form-control price" type="text" th:value="${detail.money}" th:attr="data-dictvalue=${detail.value},data-dictlabel=${detail.label}" >
                    </div>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>可先填写100的输入框，其它价格按照比例回显</span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" th:checked="*{status == 0}">
                        <span></span>
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">所属用户：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="userName" th:field="*{userName}" onclick="selectUser()" type="text" placeholder="请选择所属用户" class="form-control" required>
                        <input name="userId" type="hidden" th:field="*{userId}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        let prefix = ctx + "business/base";
        $("#form-base-edit").validate({
            focusCleanup: true
        });

        // 充值面额
        let rechargeMoneyDatas = [[${@dict.getType('sd_recharge_money')}]];
        // 铜板面额
        let copperMoneyDatas = [[${@dict.getType('sd_copper_money')}]];
        // 钢镚面额
        let coinsMoneyDatas = [[${@dict.getType('sd_coins_money')}]];

        $('select[name=businessType]').change(function () {
            if ($(this).val() != 0) {
                $('select[name=rechargeChannel]').parent().parent().hide();
            } else {
                $('select[name=rechargeChannel]').parent().parent().show();
            }

            // 后续业务直接这里续写else if
            if ($(this).val() == 0) {
                appendDiv(rechargeMoneyDatas);
            } else if ($(this).val() == 1) {
                appendDiv(copperMoneyDatas);
            } else if ($(this).val() == 2) {
                appendDiv(coinsMoneyDatas);
            } else {
                $('.priceDiv').empty().append('<input name="price" class="form-control" type="text" value="'+$('.priceDiv').data('price')+'">');
            }

        });

        function appendDiv(datas) {
            let bdatas = [];
            $.each(datas, function (index, dict) {
                bdatas.push('<div class="col-sm-4">' +
                    '                        <label class="control-label">'+dict.dictLabel+'</label><input class="form-control price" data-dictValue="'+dict.dictValue+'" data-dictLabel="'+dict.dictLabel+'" type="text">' +
                    '                    </div>')
            });
            bdatas.push('<span class="help-block m-b-none"><i class="fa fa-info-circle"></i>可先填写100的输入框，其它价格按照比例回显</span>');
            $('.priceDiv').empty().append(bdatas.join(''));
        }

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-base-edit').serializeArray();
                var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
                data.push({name: "status", value: status});
                if ($('.price').length != 0) {
                    data.push({name: 'price', value: $('.price').map(function(){return $(this).data('dictvalue') + '->' +$(this).val()}).get().reduce(function(a,b){return a + ';' + b})});
                    let idx = 0;
                    $('.price').map(function () {
                        data.push({name: 'prices['+idx+'].label', value: $(this).data('dictlabel')});
                        data.push({name: 'prices['+idx+'].value', value: $(this).data('dictvalue')});
                        data.push({name: 'prices['+idx+'].money', value: $(this).val()});
                        idx++;
                        if (!$(this).val()) {
                            $.modal.alertError($(this).data('dictlabel') + '业务价格不能为空');
                            throw new Error($(this).data('dictlabel') + '业务价格不能为空');
                        }
                    })
                }
                $.operate.save(prefix + "/edit", data);
            }
        }

        function selectUser() {
            $.modal.open('选择用户', prefix + '/selectUser');
        }

        $('.priceDiv').on('input', 'input', function () {
            if ($(this).prev().text() == '100.00') {
                let rate = parseFloat($(this).val()) / parseFloat($(this).prev().text());
                let clearFlag = $(this).val() == '' || isNaN($(this).val());
                $(this).parent().parent().find('label:not(:contains("100.00"))').each(function () {
                    let args = (parseFloat($(this).text()) * rate).toFixed(2);
                    if (clearFlag) {
                        args = '';
                    }
                    $(this).next().val(args);
                })
            }
        });
    </script>
</body>
</html>